package com.aduerr.v1shouyin.view;

import android.view.View;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.DatabaseManager;
import com.aduerr.v1shouyin.entity.LoginAccountEntity;
import com.aduerr.v1shouyin.mvp.adpter.LoginAccountAdapter;
import com.aduerr.v1shouyin.mvp.base.BasePopupWindow;

import java.util.List;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/5/9 13:38
 * @change
 * @chang time
 * @class describe
 */

public class AccountPopup extends BasePopupWindow {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private FragmentActivity mActivity;
    private LoginAccountAdapter loginAccountAdapter;
    private OnItemClickListener onItemClickListener;

    public AccountPopup(FragmentActivity mActivity) {
        super(mActivity);
        this.mActivity = mActivity;
        setContentView(R.layout.popup_account, MATCH_PARENT, WRAP_CONTENT, true);
        initView();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(mActivity));

        List<LoginAccountEntity> list =
                DatabaseManager.getInstance().getQueryAllByDesc(LoginAccountEntity.class, "id");

        loginAccountAdapter = new LoginAccountAdapter(mActivity, list);
        loginAccountAdapter.setOnItemClickListener(new LoginAccountAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                onItemClickListener.onItemClick(list.get(position));
                dismiss();
            }

            @Override
            public void onDeleteClick(View view, int position) {
                DatabaseManager.getInstance().deleteWhere(LoginAccountEntity.class,
                        "account", new String[]{list.get(position).getAccount()});
                loginAccountAdapter.delete(position);
                dismiss();
            }
        });
        recyclerview.setAdapter(loginAccountAdapter);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(LoginAccountEntity entity);
    }

}
